const db = require('../models');
const { CODE_ERROR, CODE_SUCCESS, STATUS_SUCCESS, STATUS_ERROR } = require('../../utils/constant');
const { raw } = require('body-parser');
//同步
// db.sequelize.drop();
// db.sequelize.sync({ alter: true }).then(() => {
//     console.log('同步成功')
// });
//创建实例化
// db.User.create({ firstName: 'hike', lastName: 'john', age: 22 }).then(user => {
//     console.log(user.toJSON())
// });

//按照指定字段保存
// user.save({ fields: ['firstName'] }).then((value) => {
//     console.log(value.toJSON());
// })

//递增和递减数值
//     user.increment('age', { by: 2 }).then(value => {
//         console.log(value.toJSON());
//     })
// });

//output:
/**
 * {
  bar: 2022-02-09T03:00:03.213Z,
  user_id: 3,
  firstName: '王龙超',
  lastName: '王超',
  email: '1991242252@qq.com',
  updatedAt: 2022-02-09T03:00:03.214Z,
  createdAt: 2022-02-09T03:00:03.214Z
}
 */
//简单查询
// db.User.findAll().then(value => {
//     return value.map(element => {
//         return {...element.dataValues, bar: element.bar }
//     })
// }).then(value => {
//     console.log(value)
// })

//按照字段查询
// db.User.findAll().then(array => {
//     console.log(array)
// })
// db.User.create({
//         username: 'admin1',
//         password: '1234567',
//         email: '1991242252@qq.com',
//     }).then(value => {
//         console.log(value.toJSON())
//     })
// db.Ship.create({ name: '泰坦尼克号', caption_id: 1 }).then(value => {
//     console.log(value.toJSON());
// })
// db.Ship.create({ name: '幽冥潜艇', caption_id: 1 }).then(value => {
//     console.log(value.toJSON());
// })
//更新成功返回时数组对象[2],失败时返回数组对象[0]
// db.User.update({ password: '654321' }, {
//     where: {
//         username: 'admin1'
//     }
// }).then(value => {
//     console.log(typeof(value))
// })

class userService {
    constructor() {}
        //通过用户名查询用户信息
    async findUserByUsername(username) {
        const result = await db.User.findOne({
            where: {
                username
            },
            include: db.Role,
        }).then(rawValue => {
            if (!rawValue) return
            return rawValue.toJSON()
        })
        return result
    };
    //创建用户信息
    async insertUserInfo(params) {
        const result = await db.User.create({ username: params.username, password: params.password }, {
            raw: true
        });
        return result.toJSON()
    };
    //校验用户名和密码
    async validateUser(username, password) {
        const result = await db.User.findOne({
            where: {
                username,
                password
            },
        });
        if (!result) return false
        return true
    };
    //修改密码
    async updateUserPassword(username, password) {
        const result = await db.User.update({ password }, {
            where: {
                username
            },
            raw: true
        }).then(rawValue => {
            if (!rawValue[0]) return false
            return true
        });
        return result
    }
}


module.exports = userService = new userService();